<template>
  <div class="settings-container">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <div>
            <el-button type="primary" @click="addRoles">添加角色</el-button>
            <!-- 表格 -->
            <el-table
              :data="tableData"
              border
              style="width: 100%; margin: 20px 0"
              :default-sort="{ prop: 'name', order: 'descending' }"
            >
              <el-table-column fixed type="index" label="序号" width="150" />
              <el-table-column prop="name" label="角色名" sortable />
              <el-table-column prop="description" label="描述" sortable />
              <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                  <el-button
                    type="danger"
                    size="small"
                    @click="handleClick(scope.row.id)"
                  >分配权限</el-button>
                  <el-button
                    type="primary"
                    size="small"
                    @click="handleEdit(scope.row)"
                  >修改</el-button>
                  <el-button type="warning" size="small">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
              :current-page.sync="paramsObj.page"
              :page-size="paramsObj.pagesize"
              layout="slot, prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            >共{{ total }}条</el-pagination>
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-form ref="form" :model="companyForm" label-width="80px">
            <el-form-item label="企业名称">
              <el-input v-model="companyForm.name" disabled />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="companyForm.companyAddress" disabled />
            </el-form-item>
            <el-form-item label="公司电话">
              <el-input v-model="companyForm.companyPhone" disabled />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="companyForm.mailbox" disabled />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="companyForm.remarks" disabled />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>

      <roleDig
        :dialog-form-visible.sync="dialogFormVisible"
        :is-edit="isEdit"
        :row="row"
        @success="success"
      />
      <el-dialog title="分配权限" :visible.sync="showDialogAssign">
        <assignPermission
          v-if="showDialogAssign"
          :role-id="roleId"
          @close="close"
        />
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getCompanyInfoByIdApi, getRolesListApi } from '@/api/settings'
import roleDig from '@/views/settings/components/index.vue'
import assignPermission from './assignPermission'
export default {
  components: {
    roleDig,
    assignPermission
  },
  data() {
    return {
      activeName: 'first',
      companyForm: {
        name: '',
        companyAddress: '',
        companyPhone: '',
        mailbox: '',
        remarks: ''
      },
      tableData: [],
      paramsObj: {
        page: 1,
        pagesize: 5
      },
      dialogFormVisible: false,
      isEdit: false,
      row: {},
      total: 0,
      showDialogAssign: false,
      roleId: ''
    }
  },
  created() {
    this.getCompanyInfo()
    this.getRolesList()
  },
  methods: {
    handleClick(id) {
      this.showDialogAssign = true
      this.roleId = id
    },
    close() {
      this.showDialogAssign = false
    },
    /* 获取公司信息 */
    async getCompanyInfo() {
      const res = await getCompanyInfoByIdApi(
        this.$store.state.user.userInfo.companyId //  传递id
      )
      this.companyForm = res.data
    },
    // 获取角色信息
    async getRolesList() {
      const res = await getRolesListApi(this.paramsObj)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    // 点击添加角色弹出对话框
    addRoles() {
      this.isEdit = false
      this.dialogFormVisible = true
      // 清空对话框
      this.row = {
        name: '',
        description: ''
      }
    },
    // 立即刷新
    success() {
      this.getRolesList()
    },
    // 点击编辑弹出对话框
    handleEdit(row) {
      console.log(row, 22222)
      this.dialogFormVisible = true
      this.isEdit = true
      this.row = row
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.paramsObj.page = val
      this.getRolesList()
    }
  }
}
</script>
<style lang="scss" scoped>
.settings-container {
  .el-form {
    width: 500px;
    margin-left: 100px;
  }
  .el-pagination {
    text-align: right;
    line-height: 28px;
    font-weight: 400;
    font-size: 14px;
  }
}
</style>
